﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Modelling.Measurement;

namespace ITAP.ShprossesBuilder.Editor
{
    public delegate void DimensionsOptionsChanged();

    public partial class DimensionsOptions : UserControl
    {
        public event DimensionsOptionsChanged OptionsChanged;
        private DimensionsPreferences currentDimensionsOptions;
        private DimensionPreferences currentDimensionOptions;

        public DimensionsOptions()
        {
            InitializeComponent();
        }

        public void ShowOptions(DimensionsPreferences options)
        {
            this.currentDimensionsOptions = options;
            this.currentDimensionOptions = this.currentDimensionsOptions.ModelDimensions;
            comboBoxDimensionType.Items.Clear();
            comboBoxDimensionType.Items.Add(options.ModelDimensions);
//            comboBoxDimensionType.Items.Add(options.ShprossDimensions);
            comboBoxDimensionType.Items.Add(options.SpacerDimensions);
            comboBoxDimensionType.Items.Add(options.PinHolesDimensions);

            comboBoxDimensionType.SelectedIndex = 0;
            this.UpdateControls();
        }

        private void buttonSelectFont_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() != DialogResult.OK)
                return;
            this.currentDimensionsOptions.Font = fontDialog.Font;
            this.UpdateControls();
            if (this.OptionsChanged != null)
                this.OptionsChanged.Invoke();
        }

        private void UpdateControls()
        {
            textBoxFont.Text = this.currentDimensionsOptions.Font.FontFamily.Name + ", " + 
                               this.currentDimensionsOptions.Font.SizeInPoints.ToString() + " pt.";
            switch ((comboBoxDimensionType.SelectedItem as DimensionPreferences).DimensionType)
            {
                case DimensionType.OuterDimension:
                    comboBoxInnerOuterType.Enabled = true;
                    comboBoxInnerOuterType.SelectedIndex = 0;
                    break;
                case DimensionType.InnerDimension:
                    comboBoxInnerOuterType.Enabled = true;
                    comboBoxInnerOuterType.SelectedIndex = 1;
                    break;
                case DimensionType.NotDefined:
                    comboBoxInnerOuterType.Enabled = false;
                    break;
            }
        }

        private void comboBoxDimensionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.currentDimensionOptions = comboBoxDimensionType.SelectedItem as DimensionPreferences;
            this.UpdateControls();
        }

        private void comboBoxInnerOuterType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBoxInnerOuterType.Text)
            {
                case "Внутренние размеры":
                    this.currentDimensionOptions.DimensionType = DimensionType.InnerDimension;
                    break;
                case "Внешние размеры":
                    this.currentDimensionOptions.DimensionType = DimensionType.OuterDimension;
                    break;
            }
            if (this.OptionsChanged != null)
                this.OptionsChanged.Invoke();
        }   
    }
}
